<template>
  <n-card :bordered="false" class="proCard">
    <BasicForm
      :showAdvancedButton="false"
      @register="register"
      @submit="handleSubmit"
      @reset="handleReset"
    >
      <template #statusSlot="{ model, field }">
        <n-input v-model:value="model[field]" />
      </template>
    </BasicForm>

    <BasicTable
      :columns="columns"
      :request="loadDataTable"
      :row-key="(row: ListData) => row.id"
      ref="actionRef"
      :actionColumn="actionColumn"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
      <template #tableTitle>
        <n-button type="primary" @click="addTable"> 导出 </n-button>
      </template>

      <template #toolbar>
        <n-button type="primary" @click="reloadTable">刷新数据</n-button>
      </template>
    </BasicTable>

    <n-modal v-model:show="showModal" :show-icon="false" preset="dialog" title="">
      <div class="flex">
        <n-image class="mr-5 header" width="64" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
        <div class="flex flex-col flex-row justify-between">
          <div class="flex">
            <span>宋佳佳/大小姐</span>
            <span>地区</span>
          </div>
          <div class="flex">
            <span class="mr-5">ID:111111</span>
            <span class="mr-5">电话:11111111111</span>
            <span class="mr-5">邮箱:111111111</span>
          </div>
        </div>
      </div>
      <div>
        <div class="flex mt-5 mb-5 items-center">
          <span style="font-size:18px">图片：</span>
          <span>数量:25</span>
        </div>
        <div>
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
            <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
            <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
           <n-image class="mr-5 mt-2" width="100" src="https://07akioni.oss-cn-beijing.aliyuncs.com/07akioni.jpeg" />
        </div>
      </div>
      <div>
        <div class="flex mt-5 mb-5 items-center">
         <span style="font-size:18px">出行：</span>
          <span>次数:25</span>
        </div>
        <div>
       <BasicTable
      :columns="columnsModal"
      :request="loadDataTable"
      :row-key="(row: ListData) => row.id"
      ref="actionRef"
      @update:checked-row-keys="onCheckedRow"
      :scroll-x="1090"
    >
    </BasicTable>
        </div>
      </div>
      <template #action>
        <n-space>
          <n-button type="info" @click="() => (showModal = false)">确定</n-button>
        </n-space>
      </template>
    </n-modal>
  </n-card>
</template>

<script lang="ts" setup>
  import { h, reactive, ref } from 'vue';
  import { BasicTable, TableAction } from '@/components/Table';
  import { BasicForm, FormSchema, useForm } from '@/components/Form/index';
  import { getTableList } from '@/api/table/list';
  import { columns, ListData } from './columns';
  import { PlusOutlined } from '@vicons/antd';
  import { useRouter } from 'vue-router';
  import { type FormRules } from 'naive-ui';

  const rules: FormRules = {
    name: {
      trigger: ['blur', 'input'],
      message: '请输入名称',
    },
    role: {
      type: 'number',
      message: '请选择所属角色',
      trigger: 'change',
    },
  };
  const roleList = [
    {
      label: '超级管理员',
      value: 1,
    },
    {
      label: '普通管理员',
      value: 2,
    },
  ];
  const columnsModal=ref([
    {
    title: '起点',
    key: 'name',
    width: 100,
  },
  {
    title: '终点',
    key: 'name',
    width: 100,
  },
  {
    title: '起点时间',
    key: 'name',
    width: 100,
  },
  {
    title: '终点时间',
    key: 'name',
    width: 100,
  }
  ])

  const schemas: FormSchema[] = [
    {
      field: 'type',
      component: 'NSelect',
      label: '账号种类',
      componentProps: {
        placeholder: '请选择',
        options: [
          {
            label: '超级管理员',
            value: 1,
          },
          {
            label: '普通管理员',
            value: 2,
          },
        ],
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
    {
      field: 'makeDate',
      component: 'NDatePicker',
      label: '注册时间',

      componentProps: {
        placeholder: '请选择',

        type: 'date',
        clearable: true,
        onUpdateValue: (e: any) => {
          console.log(e);
        },
      },
    },
  ];

  const router = useRouter();
  const formRef: any = ref(null);
  const actionRef = ref();

  const showModal = ref(false);
  const formBtnLoading = ref(false);
  const formParams = reactive({
    name: '',
    role: null,
    droit: true,
  });

  const actionColumn = reactive({
    width: 220,
    title: '操作',
    key: 'action',
    fixed: 'right',
    render(record) {
      return h(TableAction as any, {
        style: 'text',
        actions: [
          {
            label: '封号',
            onClick: handleEdit.bind(null, record),
            ifShow: () => {
              return true;
            },
            auth: ['basic_list'],
          },
          {
            label: '解禁',
            onClick: handleEdit.bind(null, record),
            ifShow: () => {
              return true;
            },
            auth: ['basic_list'],
          },
          {
            label: '详情',
            onClick: handleDetail.bind(null, record),
            ifShow: () => {
              return true;
            },
            auth: ['basic_list'],
          },
        ],
      });
    },
  });

  const [register, { getFieldsValue }] = useForm({
    gridProps: { cols: '1 s:1 m:2 l:3 xl:4 2xl:4' },
    labelWidth: 80,
    schemas,
  });

  // 滑块切换添加权限状态
  function handleChange(value: boolean) {
    console.log(value);
  }

  const loadDataTable = async (res) => {
    return await getTableList({ ...getFieldsValue(), ...res });
  };

  function onCheckedRow(rowKeys) {
    console.log(rowKeys);
  }

  function reloadTable() {
    actionRef.value.reload();
  }

  function confirmForm(e) {
    e.preventDefault();
    formBtnLoading.value = true;
    formRef.value.validate((errors) => {
      if (!errors) {
        window['$message'].success('新建成功');
        setTimeout(() => {
          showModal.value = false;
          reloadTable();
        });
      } else {
        window['$message'].error('请填写完整信息');
      }
      formBtnLoading.value = false;
    });
  }

  function handleEdit(record: Recordable) {
    console.log('点击了编辑', record);
    router.push({ name: 'basic-info', params: { id: record.id } });
  }

  function handleDelete(record: Recordable) {
    console.log('点击了删除', record);
    window['$message'].info('点击了删除');
  }

  function handleSubmit(values: Recordable) {
    console.log(values);
    reloadTable();
  }

  function handleReset(values: Recordable) {
    console.log(values);
  }
  const handleDetail = () => {
    showModal.value = true;
  };
</script>

<style lang="less" scoped>
  :deep(.header) {
    border-radius: 50% !important;
  }
</style>
